/*
 * TubeTile.java
 *
 * Created on 22. Dezember 2006, 23:04
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.map.tile;

import com.jme.math.FastMath;
import com.jme.math.Matrix3f;
import com.jme.math.Vector3f;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Cylinder;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Pirx
 */
public class TubeTile extends AbstractTile {
    
    private static final float[] RADIUS = new float[]{0, 0.8f, 0.4f, 0.3f, 0.2f };
    private static final int[] SAMPLES = new int[]{0, 25, 15, 15, 15};
    private static final float[][][] COORDS = new float[][][]{
        {{}},
        {{0, 0}},
        {{0.5f, 0.5f}, {-0.5f, -0.5f}},
        {{0.4f, -0.6f}, {-0.4f, -0.6f}, {0, 0.5f}},
        {{-0.6f, -0.6f}, {-0.6f, 0.6f}, {0.6f, -0.6f}, {0.6f, 0.6f},}
    };
    
    private static final String RENDERSTATE = "renderstate";
    private static final String TUBES = "tubes";
    private static final String TYPE = "type";
    
    private String renderstate;
    private TriMesh[] meshes;
    private Type type;
    
    public enum Type {NS, OW};
    
    private final static Matrix3f ROT_MATRIX = new Matrix3f();
    
    static {
        ROT_MATRIX.fromAngleAxis(FastMath.PI/2, new Vector3f(0,1,0)); 
    }
    
    /** Creates a new instance of TubeTile
     * @param name
     * @param ref
     * @param type
     * @param tubes
     * @param texture
     */
    public TubeTile(String name, char ref,  Type type, int tubes, String texture) {
        super(name, ref);
        this.renderstate = renderstate;
        this.meshes = new TriMesh[tubes];
        this.type = type;
    }
    
    public TubeTile() {
    }
    
    public void init() {
        for (int i = 0; i < getSize(); i++) {
            meshes[i] = new Cylinder("cyl", 3, SAMPLES[getSize()], RADIUS[getSize()], 2, false);
            meshes[i].setLocalTranslation(new Vector3f(COORDS[getSize()][i][0], COORDS[getSize()][i][1], 0));
            if (type == Type.OW) {
                meshes[i].setLocalRotation(ROT_MATRIX);
            }        
        }
    }
    
    public void setParams(Map<String, String> map) {
        renderstate = map.get(RENDERSTATE);
        meshes = new TriMesh[Integer.parseInt(map.get(TUBES))];
        type = Type.valueOf(map.get(TYPE));
    }
    
    public Map<String, String> getParams() {
        Map<String, String> map = new HashMap<String, String>();
        map.put(RENDERSTATE, renderstate);
        map.put(TUBES, "" + getSize());
        map.put(TYPE, type.toString());
        return map;
    }
    
    public TriMesh getMesh(int index) {
        if (meshes == null) {
            meshes = new TriMesh[1];
        }
        if (meshes[0] == null) {
            init();
        }
        return meshes[index];
    }
    
    public String getRenderStateName(int index) {
        return renderstate;
    }
    
    public int getSize() {
        return meshes.length;
    }
    
}
